/**
 * @(#)AddReserveTaskTimer.java 2017年09月26日
 * Copyright(C) 2016 ZHEXIN IT CO.,LTD. All rights reserved.
 */
package org.jsbd.boss.timer;

import javax.annotation.Resource;

import org.jsbd.boss.service.IReserveAddService;
import org.jsbd.boss.service.IReserveAddUserService;
import org.jsbd.boss.service.IReserveInstallService;
import org.jsbd.boss.util.CacheKey;
import org.jsbd.boss.util.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.Set;

/**
 * 
 * 预装用户定时器
 * 
 * @author <a href="mailto:xupengcheng@zhexinit.com" >徐鹏程</a>
 * @version 1.0.0
 */
@Service
public class AddReserveTaskTimer extends TaskAdapterHandler {
	public static final Logger LOGGER = LoggerFactory.getLogger(AddReserveTaskTimer.class);

	@Resource
	private IReserveAddService reserveAddService;
	@Resource
	private IReserveInstallService reserveInstallService;
	@Resource
	private IReserveAddUserService addUserService;

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jsbd.boss.timer.TaskHandler#execute()
	 */
	@Override
	public void run() {
		long expireTime = 60;
		Object refreshTime = cacheParamManager.getParamValue(CacheKey.T_L_REFRESH);
		if (refreshTime != null) {
			expireTime = Long.parseLong(refreshTime.toString());
		}
		String initDataLockKey = CacheKey.RESERVE_TIMER_LOCK;
		if (redisClient.evalSetNx(initDataLockKey, 120)) {
			long now = System.currentTimeMillis();
			reserveInstallService.initInstallUserData();
			reserveAddService.initReserveAddUserData();
			LOGGER.warn("预新增定时器更新花费时间 {} 秒", ((System.currentTimeMillis() - now) / 1000));
			redisClient.remove(initDataLockKey);
		}
		if (redisClient.evalSetNx(initDataLockKey + 1, expireTime)) {
			long now = System.currentTimeMillis();
			addUserService.initReserveAddTask();
			LOGGER.warn("预新增用户任务更新花费时间 {} 秒", ((System.currentTimeMillis() - now) / 1000));
		}
	}

}
